package com.xinjian.jianba.aliyun;

import java.util.List;

import org.junit.Test;

import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;

public class AliVideo {
	private static String videoId = "941cac14766f4db3b8bfe041996e6748";
//	private static String accessKeyId = "STS.NHHxZsyYJAuPZY2Z9GUJCBHrF";
//	private static String accessKeySecret = "941cac14766f4db3b8bfe041996e6748";
	private static  String accessKeyId = "LTAI4MkXFoeiYpbu";
	private static String accessKeySecret = "c4ppw6jeum5IDJWNzDT6mYLRjH2J5E";

//    String accessKeyId = "LTAI4MkXFoeiYpbu";
//    String accessKeySecret = "c4ppw6jeum5IDJWNzDT6mYLRjH2J5E";
	
	public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) {
		//点播服务所在的Region，国内请填cn-shanghai，不要填写别的区域
		String regionId = "cn-shanghai";
		DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
		DefaultAcsClient client = new DefaultAcsClient(profile);
		return client;

	}

	/*获取播放地址函数*/
	public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient client) throws Exception {
		GetPlayInfoRequest request = new GetPlayInfoRequest();
		request.setVideoId(videoId);
		return client.getAcsResponse(request);
	}

	/*以下为调用示例*/
	public static void main(String[] argv) {
		getPlayAuth();
		getPlayUrl();
	}

	private static void getPlayUrl() {
		DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
		GetPlayInfoResponse response = new GetPlayInfoResponse();
		try {
			response = getPlayInfo(client);
			List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
			//播放地址
			for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
				System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
			}
			//Base信息
			System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
		} catch (Exception e) {
			System.out.print("ErrorMessage = " + e.getLocalizedMessage());
		}
		System.out.print("RequestId = " + response.getRequestId() + "\n");
	}

	/*获取播放凭证函数*/
	public static GetVideoPlayAuthResponse getVideoPlayAuth(DefaultAcsClient client) throws Exception {
		GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
		request.setVideoId(videoId);
		return client.getAcsResponse(request);
	}

	private static void getPlayAuth() {
		DefaultAcsClient client = initVodClient(accessKeyId, accessKeySecret);
		GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
		try {
			response = getVideoPlayAuth(client);
			//播放凭证
			System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
			//VideoMeta信息
			System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
		} catch (Exception e) {
			System.out.print("ErrorMessage = " + e.getLocalizedMessage());
		}
		System.out.print("RequestId = " + response.getRequestId() + "\n");
	}
	/*删除视频函数*/
	public static DeleteVideoResponse deleteVideo(DefaultAcsClient client,String videoIds) throws Exception {
	    DeleteVideoRequest request = new DeleteVideoRequest();
	    
	    //多个用逗号分隔，最多支持20个
//	    request.setVideoIds("视频ID1,视频ID2");
	    request.setVideoIds(videoIds);
	    return client.getAcsResponse(request);
	}
	@Test
	public void delete(){
		 DefaultAcsClient client=initVodClient(accessKeyId, accessKeySecret);
		 DeleteVideoResponse response = new DeleteVideoResponse();
		 String videoIds="f03308f65cc94b98b7dfc339d3114258,86b89a335cf9400687244c711983dafa";
		 try {
		        response = deleteVideo(client,videoIds);
		    } catch (Exception e) {
		        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
		    }
		    System.out.print("RequestId = " + response.getRequestId() + "\n");
	}
}
